Public Class frmDctnryAddMdfy

#Region " CLASS MEMBER VARIABLES "
  Private Shared mObjFrmDctnryAddMdfy As frmDctnryAddMdfy
  Private mObjClsDctnry As New clsDctnry
  Private mObjDataTable As New DataTable
  Private mStrDscrpn As String = String.Empty
  Private mStrAbrvtn As String = String.Empty
  Private mStrSearch As String = String.Empty
#End Region

  Private Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    Me.dgvDctnry.MultiSelect = False
  End Sub

  Public Shared Function mFnGetInstance() As frmDctnryAddMdfy
    If mObjFrmDctnryAddMdfy Is Nothing Then
      mObjFrmDctnryAddMdfy = New frmDctnryAddMdfy
    End If

    Return mObjFrmDctnryAddMdfy
  End Function

  Private Sub frmDctnryAddMdfy_Load(ByVal sender As System.Object _
                                  , ByVal e As System.EventArgs) Handles MyBase.Load


    mPrRefreshConn()
    mObjDataTable = mObjClsDctnry.mPrPopulateGrid

    dgvDctnry.Columns(1).DataPropertyName = "Description"
    dgvDctnry.Columns(2).DataPropertyName = "Abbreviation"

    dgvDctnry.DataSource = mObjDataTable

  End Sub

  Private Sub dgvDctnry_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvDctnry.CellMouseClick

    If e.RowIndex = -1 Then
      Exit Sub
    End If

    If e.ColumnIndex = 0 Then
      If MsgBox("Delete - """ & dgvDctnry.Rows(e.RowIndex).Cells("clmDesc").Value & """ ?", MsgBoxStyle.YesNo, gCONST_MSG_HEADER) = MsgBoxResult.Yes Then
        mObjClsDctnry.mPrDelete(dgvDctnry.Rows(e.RowIndex).Cells("clmDesc").Value, dgvDctnry.Rows(e.RowIndex).Cells("clmAbbr").Value)
        mObjDataTable = New DataTable
        mObjDataTable = mObjClsDctnry.mPrPopulateGrid
        dgvDctnry.DataSource = mObjDataTable
        txtDscrpn.Text = String.Empty
        txtAbrvtn.Text = String.Empty
        mStrSearch = String.Empty
      End If
    End If
  End Sub

  Private Sub dgvDctnry_GotFocus(ByVal sender As Object _
                               , ByVal e As System.EventArgs) Handles dgvDctnry.GotFocus

    timTimer.Enabled = True

  End Sub

  Private Sub dgvDctnry_KeyDown(ByVal sender As Object _
                              , ByVal e As System.Windows.Forms.KeyEventArgs _
                              ) Handles dgvDctnry.KeyDown


    Dim lIntKeyValue As Integer = e.KeyValue

    Dim lChrKey As String = Chr(lIntKeyValue).ToString

    If (Char.IsLetter(lChrKey)) Then
      mStrSearch = mStrSearch & lChrKey
      For Each lObjRow As DataGridViewRow In Me.dgvDctnry.Rows
        Dim lObjCellValue As Object = lObjRow.Cells(0).Value

        If (lObjCellValue.ToString().StartsWith(LCase(mStrSearch))) Then
          lObjRow.Selected = True

          Me.dgvDctnry.FirstDisplayedScrollingRowIndex = lObjRow.Index

          Exit For
        End If
      Next
    End If
  End Sub

  Private Sub dgvDctnry_MouseDoubleClick(ByVal sender As Object _
                                       , ByVal e As System.Windows.Forms.MouseEventArgs _
                                       ) Handles dgvDctnry.MouseDoubleClick

    txtDscrpn.Text = dgvDctnry.CurrentRow.Cells(0).Value
    txtAbrvtn.Text = dgvDctnry.CurrentRow.Cells(1).Value

    mStrAbrvtn = String.Empty
    mStrDscrpn = String.Empty

    mStrAbrvtn = dgvDctnry.CurrentRow.Cells(1).Value
    mStrDscrpn = dgvDctnry.CurrentRow.Cells(0).Value
  End Sub

  Private Sub cmdMdfy_Click(ByVal sender As System.Object _
                          , ByVal e As System.EventArgs) Handles cmdMdfy.Click


    If txtDscrpn.Text = String.Empty Then
      MessageBox.Show("Please Enter Description")
      Exit Sub
    End If

    If txtAbrvtn.Text = String.Empty Then
      MessageBox.Show("Please Enter Abbreviation")
      Exit Sub
    End If

    mObjClsDctnry.mPrDelete(mStrDscrpn, mStrAbrvtn)
    mObjClsDctnry.mPrInsert(txtDscrpn.Text, txtAbrvtn.Text)
    mObjDataTable = New DataTable
    mObjDataTable = mObjClsDctnry.mPrPopulateGrid

    dgvDctnry.DataSource = mObjDataTable

    txtDscrpn.Text = String.Empty
    txtAbrvtn.Text = String.Empty

    mStrSearch = String.Empty

  End Sub

  Private Sub cmdAdd_Click(ByVal sender As System.Object _
                         , ByVal e As System.EventArgs) Handles cmdAdd.Click

    If txtDscrpn.Text = String.Empty Then
      MessageBox.Show("Please Enter Description")
      Exit Sub
    End If

    If txtAbrvtn.Text = String.Empty Then
      MessageBox.Show("Please Enter Abbreviation")
      Exit Sub
    End If

    For Each lObjRow As DataRow In mObjDataTable.Rows
      txtDscrpn.Text = lObjRow("Description")
      MessageBox.Show("The Description already exists")
      Exit Sub
    Next
    mObjClsDctnry.mPrInsert(txtDscrpn.Text, txtAbrvtn.Text)
    mObjDataTable = New DataTable
    '
    mObjDataTable = mObjClsDctnry.mPrPopulateGrid

    dgvDctnry.DataSource = mObjDataTable
    txtDscrpn.Text = String.Empty
    txtAbrvtn.Text = String.Empty
    mStrSearch = String.Empty

  End Sub

  Private Sub cmdCancel_Click(ByVal sender As System.Object _
                            , ByVal e As System.EventArgs) Handles cmdCancel.Click

    mStrSearch = String.Empty

    Me.Dispose()

  End Sub

  Private Sub timTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timTimer.Tick

    mStrSearch = String.Empty

  End Sub

End Class